package com.chinaunicom.emergency.action;

import com.chinaunicom.emergency.annotation.Permission;
import com.chinaunicom.emergency.exception.CheckException;
import com.chinaunicom.emergency.model.AccReport;
import com.chinaunicom.emergency.service.AccReportService;
import com.chinaunicom.emergency.util.Assert;
import com.chinaunicom.emergency.util.UpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @author liuzhuang
 * @date 2020-8-5
 */
@Api(value = "/accident", tags = "事故管理接口")
@RestController
@RequestMapping("/accident")
@Log4j2
@RefreshScope
public class AccReportAction extends BaseAction {
    @Autowired
    private AccReportService accReportService;


    @ApiOperation(value = "事故录入", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @Permission("accident:add")
    @PostMapping
    public Object insert(@RequestBody AccReport accReport) {
        accReportService.insert(accReport);
        return success();
    }


    @ApiOperation(value = "事故修改", httpMethod = "PUT")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @Permission("accident:update")
    @PutMapping
    public Object update(@RequestBody AccReport accReport) throws CheckException {
        Assert.isBlank(accReport.getPid(), "PID不能为空！");
        AccReport result = accReportService.get(accReport.getPid());
        Assert.isNull(result,"数据不存在,修改失败");
        UpdateUtil.copyNullProperties(result,accReport);
        accReportService.update(accReport);
        return success();
    }

    @ApiOperation(value = "事故删除", httpMethod = "DELETE")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @Permission("accident:delete")
    @DeleteMapping
    public Object delete(@RequestBody AccReport accReport) throws CheckException {
        Assert.isBlank(accReport.getPid(), "PID不能为空！");
        AccReport result = accReportService.get(accReport.getPid());
        Assert.isNull(result,"数据不存在,删除失败");
        accReportService.delete(accReport);
        return success();
    }

    @ApiOperation(value = "事故启动", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @Permission("accident:start")
    @PostMapping("start")
    public Object start(@RequestBody AccReport accReport) throws CheckException {
        Assert.isBlank(accReport.getPid(), "PID不能为空！");
        AccReport result = accReportService.get(accReport.getPid());
        Assert.isNull(result,"数据不存在,启动失败");
        result.setRescureState(1);
        UpdateUtil.copyNullProperties(result,accReport);
        accReportService.update(result);
        return success();
    }

}
